package com.amkai.aes.webservice.binary.objecthandler;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.amkai.aes.aescommon.login.errorcodes.LoginErrorCodes;
import com.amkai.aes.aescommon.service.binary.job.LoginRelatedOperationsJob;
import com.amkai.aes.aescommon.service.result.LoginRelatedOperationsResult;
import com.amkai.aes.generatedbeans.organization.Department;
import com.amkai.aes.server.manager.login.LoginRelatedOperationsManager;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

/**
 * #12512
 * @author lkovari
 *
 */
public class LoginRelatedOperationsHandler implements JobObjectHandler<LoginRelatedOperationsJob, Serializable, LoginRelatedOperationsResult> {
	private static final Log log = LogFactory.getLog(LoginRelatedOperationsHandler.class);

	@Override
	public LoginRelatedOperationsResult process(LoginRelatedOperationsJob job, Serializable object) throws Exception {
		LoginRelatedOperationsResult result = new LoginRelatedOperationsResult();

		/*List<Department> departmentList = null;
		List<BusinessGroup> businessGroupList = null;*/
		SessionManager sm = SiteManager.Start();
		// KKORNEL: AES-12647
		LoginRelatedOperationsManager manager = new LoginRelatedOperationsManager(sm);
		
		try {
			sm.openSession();
			
			switch (job.getLoginRelatedOperationKind()) {
			case ALL_BUSINESS_GROUP : {
				// KKORNEL: AES-12647
				/*businessGroupList = sm.execute("SELECT bg FROM BusinessGroup bg WHERE EXISTS (SELECT dep.id FROM Department dep WHERE dep.parent.id = bg.id)").list();
				result.setBusinessGroups(businessGroupList);*/
				result.setBusinessGroups(manager.getAllBusinessGroups());
				break;
			}
			case BUSINESS_ENTITIES_BY_BUSINESS_GROUP : {
				// KKORNEL: AES-12647
				/*String queryText = "select be FROM Department be left join fetch be.parent where be.parent = :businesGroup";
				departmentList = sm.execute(queryText).setEntity("businesGroup", job.getBusinessGroup()).list();
				for (Department dep : departmentList) {
					if (dep.getParent() instanceof BusinessGroup) {
						BusinessGroup bg = (BusinessGroup)dep.getParent();
						bg.getName();
						bg.getKind();
						bg.getId();
						bg.getLevel();
					}
				}
				result.setDepartments(departmentList);*/
				result.setDepartments(manager.getBusinessEntitiesByBusinessGroup(job.getBusinessGroup()));
				break;
			}
			case COMPATIBLE_BUSINESS_GROUPS: {
				Department master = sm.get(Department.class, job.getModuleDepartment().getId());
				result.setBusinessGroups(manager.getCompatibleBusinessGroups(master));
				break;
			}
			case COMPATIBLE_BUSINESS_ENTITIES_BY_BUSINESS_GROUP: {
				Department master = sm.get(Department.class, job.getModuleDepartment().getId());
				result.setDepartments(manager.getCompatibleBusinessEntitiesByBusinessGroup(job.getBusinessGroup(), master));
				break;
			}
			}
			result.setLoginErrorCode(LoginErrorCodes.SUCCESS);
		}	
		catch (Exception e) {
			switch (job.getLoginRelatedOperationKind()) {
			case ALL_BUSINESS_GROUP : result.setLoginErrorCode(LoginErrorCodes.EXCEPTION_WHEN_GET_BG);
			case BUSINESS_ENTITIES_BY_BUSINESS_GROUP : result.setLoginErrorCode(LoginErrorCodes.EXCEPTION_WHEN_GET_BE_BY_BG);
			}
			result.setException(e);
		} 
		finally {
			sm.closeSession();
		}
		return result;
	}

}
